package com.olddriver.crm.web.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.olddriver.crm.util.JsonResult;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;

import javax.servlet.http.HttpServletResponse;


@ControllerAdvice
public class BaseController {
    @ExceptionHandler(UnauthorizedException.class)
    public void handlerException(HandlerMethod method, HttpServletResponse resp) throws Exception {
        resp.setCharacterEncoding("utf-8");
        if (method.getMethod().getAnnotation(ResponseBody.class) != null) {
            resp.getWriter().print(new ObjectMapper().writeValueAsString(JsonResult.diyFaliure("缺少必要的权限")));
        }else {
            resp.sendRedirect("/nopermission.jsp");
        }
    }
}
